//
//  ILFavoriteMusicListViewController.m
//  ILife
//
//  Created by lanou on 15/11/4.
//  Copyright © 2015年 ILife. All rights reserved.
//

#import "ILFavoriteMusicListViewController.h"
#import "FMDatabase.h"
#import "ILFavoriteMusicListModel.h"
#import "ILFavoriteMusicTableViewCell.h"
#import "ILMusicDetailViewController.h"
@interface ILFavoriteMusicListViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, strong)NSMutableArray *favoriteMusicArray;
@property(nonatomic, strong)FMDatabase *dataBase;
@property(nonatomic, strong)UITableView *favoriteTableView;
@end

@implementation ILFavoriteMusicListViewController

-(NSMutableArray *)favoriteMusicArray
{
    if (_favoriteMusicArray == nil) {
        self.favoriteMusicArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _favoriteMusicArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
  
     self.navigationItem.title = @"音乐收藏";
    
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"ILive.db"];
    _dataBase = [FMDatabase databaseWithPath:filePath];
    NSLog(@"filePath = %@",filePath);
    if ([_dataBase open]) {
        NSLog(@"数据库dddd打开成功");
    // [_dataBase executeUpdate:@"drop table ILMusic"];
        FMResultSet *resultSet =[_dataBase executeQueryWithFormat:@"select *from ILFavoriteMusic"];
        while ([resultSet next]) {
            NSString *musicId = [resultSet stringForColumn:@"musicId"];
            NSString *image = [resultSet stringForColumn:@"image"];
            NSString *songname = [resultSet stringForColumn:@"songname"];
            NSString *title = [resultSet stringForColumn:@"title"];
            ILFavoriteMusicListModel *favoriteModel = [[ILFavoriteMusicListModel alloc]init];
            favoriteModel.musicId = musicId;
            favoriteModel.image = image;
            favoriteModel.songname = songname;
            favoriteModel.title = title;
            [self.favoriteMusicArray addObject:favoriteModel];
        }
        if(self.favoriteMusicArray.count == 0)
        {
         
            UIAlertController *alertCollect =[UIAlertController alertControllerWithTitle:@"提示" message:@"没有收藏呢，快去收藏一个吧" preferredStyle:(UIAlertControllerStyleAlert)];
            
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:nil];
            [alertCollect addAction:okAction];
            [self presentViewController:alertCollect animated:YES completion:nil];

        }
    }else
    {
        NSLog(@"数据库ddddd打开失败");
    }
    _favoriteTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _favoriteTableView.delegate =self;
    _favoriteTableView.dataSource =self;
    
    
    [_favoriteTableView registerClass:[ILFavoriteMusicTableViewCell class] forCellReuseIdentifier:@"ILFavoriteMusicTableViewCell"];
    
    [self.view addSubview:_favoriteTableView];
    
    self.favoriteTableView.tableFooterView = [[UIView alloc]init];
    
}





-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ILFavoriteMusicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ILFavoriteMusicTableViewCell" forIndexPath:indexPath];
    ILFavoriteMusicListModel *favoriteModel = self.favoriteMusicArray[indexPath.row];
    cell.musicListModel = favoriteModel;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.favoriteMusicArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ILMusicDetailViewController *musicDetailVC = [[ILMusicDetailViewController alloc]init];
    ILFavoriteMusicListModel *favoriteModel = self.favoriteMusicArray[indexPath.row];
     musicDetailVC.musicID= favoriteModel.musicId;
   // NSLog(@"MSUICID === %@",favoriteModel.musicId);
    [self.navigationController pushViewController:musicDetailVC animated:YES];

}


//  删除
//　1.让tableview处于可编辑状态
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [_favoriteTableView setEditing:editing animated:animated];

}

//  2.设置可编辑行的编辑状态（默认是删除）
//  3.
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
     // 1.先删除数据源
    ILFavoriteMusicListModel *favoriteModel = self.favoriteMusicArray[indexPath.row];
    [_dataBase executeUpdate:@"delete from ILFavoriteMusic where musicId = ?",favoriteModel.musicId];
    
    [self.favoriteMusicArray removeObjectAtIndex:indexPath.row];

    [_favoriteTableView reloadData];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [_dataBase close];
}

















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
